﻿using Freshbox2._0.BLL;
using Freshbox2._0.MODEL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Freshbox2._0.UI.Controllers
{
    public class SupplierController : BaseController<supplier>
    {
        //
        // GET: /Supplier/
        protected admin CurrentUer
        {
            get { return Session["CurrentUer"] == null ? null : (admin)Session["CurrentUer"]; }
        }
        public override BLL.BaseBll<supplier> bll
        {
            get
            {
                return new SupplierBll();
            }

        }
        //public override ActionResult Index()
        //{
        //    return base.Index();
        //}
        public class waresModel
        {
            public string waresCode { get; set; }
            public string waresName { get; set; }
        }

        SupplierWaresBll supplierWaresBll = new SupplierWaresBll();

        //加载数据列表 查询方法
        public override ActionResult Get(int page, int rows, supplier model)
        {
            int count;
            var list = bll.Search(model, x => true, true, x => x.supplierID, rows, page, out count).ToList().Select(x => new
            {
                x.supplierID,
                x.phone,
                status = x.status == 1 ? "有效" : "无效",
                x.contactorName,
                x.company,
                x.companyAddress,
            }).ToList();
            return Json(new { total = count, rows = list });
        }
        //新增用户
        [HttpPost]
        public override ActionResult Save(supplier model)
        {
            string msg = "";
            int result = 0;
            int jieguo = 1;
            if (model.company != "" && model.contactorName != ""&&model.companyAddress!=""&&model.phone!=null)
            {
                    model.status = 1;
                    if (bll.Add(model) > 0)
                    {
                        msg = "添加成功";
                        result = 1;
                        jieguo = 1;
                        return Json(new { jieguo = jieguo, state = result, msg = msg });
                    }
                    else
                    {
                        jieguo =0;
                        msg = "添加失败";
                        return Json(new { jieguo = jieguo, state = result, msg = msg });
                    }
            }
            else
            {
                jieguo = 0;
                msg = "请填写完整";
                return Json(new { jieguo = jieguo, state = result, msg = msg });
            }
         
        }


        //获取选中的一条数据
        public ActionResult GetOnlyOne(int id)
        {
            return Json(bll.Search(x => x.supplierID == id).ToList().Select(x => new {x.supplierID, x.company, x.companyAddress, x.contactorName,x.phone }).ToList()[0], JsonRequestBehavior.AllowGet);
        }
        WaresBll waresBll = new WaresBll();
        /// <summary>
        /// 获取该供应商 对应的商品信息
        /// </summary>
        /// <param name="supplierID"></param>
        /// <returns></returns>
        public ActionResult GetDetailByID(int supplierID)
        {
            var waresModelList = new List<waresModel>();

            var codelist = supplierWaresBll.Search(x => x.supplierID == supplierID).Select(x => new { x.supplierID, x.waresCode }).ToList();

            foreach (var item in codelist)
	        {
                string name = waresBll.Search(x => x.waresCode == item.waresCode).Select(x => new { x.waresName }).FirstOrDefault().waresName;
                waresModel model = new waresModel();
                model.waresCode = item.waresCode;
                model.waresName = name;
                waresModelList.Add(model);
	        }
            return Json(waresModelList);
        }



        /// <summary>
        /// 修改 供应商信息 
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>

        public override ActionResult Modify(supplier model)
        {
            string msg = "修改失败";
            string state = "false";
            model.status = 1;
            if (model.company != "" && model.contactorName != "" && model.companyAddress != "" && model.phone != "")
            {
                if (bll.Modify(model) > 0)
                {
                    msg = "修改成功";
                    state = "true";
                }
            }
            else
            {
                msg = "请填写完整";
                return Json(new { state = state, msg = msg });
            }

            return Json(new { state = state, msg = msg });
        }

        //禁用用户  修改状态为0
        public ActionResult ModifyState(int id)
        {
            string msg = "禁用失败";
            string state = "false";
            var oneList = bll.Search(x => x.supplierID == id).Select(x => new { x.supplierID, x.status, x.phone,x.companyAddress,x.company,x.contactorName }).ToList();
            supplier model = new supplier();
            model.supplierID = id;
            model.phone = oneList.FirstOrDefault().phone;
            model.companyAddress = oneList.FirstOrDefault().companyAddress;
            model.company = oneList.FirstOrDefault().company;
            model.contactorName = oneList.FirstOrDefault().contactorName;
            model.status = 0;

            if (bll.Modify(model) > 0)
            {
                msg = "禁用成功";
                state = "true";
            }
            return Json(new { state = state, msg = msg });
        }
    }
}
